package com.peng.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.peng.common.R;
import com.peng.entity.ShoppingCart;
import com.peng.service.ShoppingCartService;
import com.peng.utils.BaseContextUtils;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        Long id = BaseContextUtils.getId();
        shoppingCart.setUserId(id);
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,id);
        Long dishId = shoppingCart.getDishId();
        if (dishId!=null){
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
//            queryWrapper.eq(ShoppingCart::getDishFlavor,shoppingCart.getDishFlavor());
        }else {
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart one = shoppingCartService.getOne(queryWrapper);
        if (one==null){
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);
            one=shoppingCart;
        }else {
            one.setNumber(one.getNumber()+1);
            shoppingCartService.updateById(one);
        }
        return R.success(one);
    }

    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContextUtils.getId());
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);
    }

    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart){
        Long dishId = shoppingCart.getDishId();
        if (dishId!=null){
            LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
            queryWrapper.eq(ShoppingCart::getUserId,BaseContextUtils.getId());
            ShoppingCart one = shoppingCartService.getOne(queryWrapper);
            int i = one.getNumber();
            if (i>0){
                one.setNumber(one.getNumber()-1);
                if (one.getNumber()>0){
                    shoppingCartService.updateById(one);
                }else {
                    shoppingCartService.remove(new LambdaQueryWrapper<ShoppingCart>().eq(ShoppingCart::getDishId,dishId));
                }
            }else if (i==0){
                shoppingCartService.remove(new LambdaQueryWrapper<ShoppingCart>().eq(ShoppingCart::getDishId,dishId));
            }else {
                return R.error("移除错误");
            }
            return R.success("移除成功");
        }
        Long setmealId = shoppingCart.getSetmealId();
        if (setmealId!=null){
            LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ShoppingCart::getSetmealId,setmealId);
            queryWrapper.eq(ShoppingCart::getUserId,BaseContextUtils.getId());
            ShoppingCart one = shoppingCartService.getOne(queryWrapper);
            Integer number = one.getNumber();
            if (number>0){
                one.setNumber(number-1);
                if (one.getNumber()>0){
                    shoppingCartService.updateById(one);
                }else {
                    shoppingCartService.remove(new LambdaQueryWrapper<ShoppingCart>().eq(ShoppingCart::getSetmealId,setmealId));
                }
            }else if (number==0){
                shoppingCartService.remove(new LambdaQueryWrapper<ShoppingCart>().eq(ShoppingCart::getSetmealId,setmealId));
            }else {
                return R.error("移除错误");
            }
            return R.success("移除成功");
        }

        return R.success("移除成功");
    }

    @DeleteMapping("/clean")
    public R<String> clean(){
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContextUtils.getId()) ;
        shoppingCartService.remove(queryWrapper);
        return R.success("删除成功");
    }
}
